package com.heima.corss.controller;

/*
    线程安全的容器有哪些？
        1. 字符串容器  -- StringBuffer(安全)     StringBuilder(不安全)
        2. list容器  --   Vector(安全-悲观锁实现) CopyOnWriteArrayList(安全-乐观锁实现)    ArrayList(不安全)
        1. Set容器  -- CopyOnWriteArraySet(安全-乐观锁实现)     HashSet(不安全)
        1. map容器  -- Hashtable(安全-悲观锁实现) ConcurrentHashMap(安全-乐观锁实现)    HashMap(不安全)

 */

import java.util.HashMap;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;

public class CollectionDemo {
    public static void main(String[] args) throws InterruptedException {
        //HashMap<String, String> map = new HashMap<>();
        //Hashtable<String, String> map = new Hashtable<>();
        ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();


        for (int i = 0; i < 50; i++) {
            int j = i;
            new Thread(() -> {
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                map.put(j + "", j + "");
            }).start();
        }


        Thread.sleep(50);

        System.out.println(map.size());
        map.forEach((key, value) -> System.out.println(key + "--->" + value));

    }
}
